package com.pcbly.web.persistence.dict;

import java.util.List;

import org.hibernate.Query;

import com.pcbly.web.bean.dict.DictCategoryBean;
import com.pcbly.web.bean.dict.DictCategoryFilterBean;
import com.pcbly.web.bean.dict.DictCategoryValueBean;
import com.pcbly.web.persistence.HibernateBaseDao;

@SuppressWarnings("unchecked")
public class DictCategoryFilterHibernateDao extends
		HibernateBaseDao<DictCategoryFilterBean> implements
		DictCategoryFilterDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 943440771234425565L;

	public DictCategoryFilterHibernateDao() {
		super();
	}

	protected Class<DictCategoryFilterBean> getEntityClass() {
		return DictCategoryFilterBean.class;
	}

	@Override
	public void removeDictCategoryFilterByDictId(String dictId)
			throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("delete c from ");
		sb.append(" sys_dict_filter c ");
		sb.append(" where ");
		sb.append(" c.MASTERID in ('");
		sb.append(dictId.replaceAll(",", "','"));
		sb.append("') ");
		getSession().createSQLQuery(sb.toString())
				.executeUpdate();

	}

	@Override
	public List<DictCategoryBean> queryDictCategoryBeansByMasterId(
			String masterId) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select a ");
		sb.append(" from DictCategoryBean a,DictCategoryDependBean b  ");
		sb.append(" where a.id=b.childId and b.masterId=? ");
		return getHibernateTemplate().find(sb.toString(), masterId);
	}

	@Override
	public List<DictCategoryValueBean> queryDictCategoryValueBeansByBean(
			DictCategoryFilterBean bean) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select a ");
		sb.append(" from DictCategoryValueBean a,DictCategoryFilterBean b  ");
		sb.append(" where a.id=b.childValueId and ");
		sb.append(" b.masterId=? and ");
		sb.append(" b.masterValueId=? and ");
		sb.append(" b.childId=?  ");
		Query query = getSession().createQuery(sb.toString());
		query.setString(0, bean.getMasterId());
		query.setString(1, bean.getMasterValueId());
		query.setString(2, bean.getChildId());

		return query.list();
	}

	@Override
	public void removeDictCategoryFilterByDictValueId(String dictValueId)
			throws Exception {
		// TODO Auto-generated method stub
		getSession().createQuery(
				"delete from DictCategoryFilterBean c where c.masterValueId=?")
				.setString(0, dictValueId).executeUpdate();
	}

	@Override
	public void removeDictCategoryFilterByDictValueIdAndDictId(
			String dictValueId, String dictId) throws Exception {
		// TODO Auto-generated method stub
		getSession()
				.createQuery(
						"delete from DictCategoryFilterBean c where c.masterId=? and c.masterValueId=?")
				.setString(0, dictId).setString(1, dictValueId).executeUpdate();
	}

	@Override
	public List<DictCategoryValueBean> queryDictCategoryValueBeansByMasterValueId(
			String masterValueId, String childId) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select a ");
		sb.append(" from DictCategoryValueBean a,DictCategoryFilterBean b  ");
		sb
				.append(" where a.id=b.childValueId and b.childId=? and b.masterValueId=? ");
		return getSession().createQuery(sb.toString()).setString(0, childId)
				.setString(1, masterValueId).list();
	}

}
